﻿<Page x:Class="Wokhan.WindowsFirewallNotifier.Console.UI.Pages.Map"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:maps="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:Wokhan.WindowsFirewallNotifier.Console.UI.Pages"
      mc:Ignorable="d" 
      d:DesignHeight="400" d:DesignWidth="600"
      DataContext="{Binding RelativeSource={RelativeSource Self}}"
      Title="Map">
    <DockPanel>
        <Border Background="#FFFF7B7B" Style="{StaticResource NonAdminPanel}">
            <WrapPanel>
                <Label Content="Information provided here might be inaccurate for non-admin users." />
                <Button x:Name="btnRestartAdmin" Click="btnRestartAdmin_Click">
                    <Grid>
                        <Path Height="16" HorizontalAlignment="Left" Stretch="Uniform" Fill="#FF5353F7" Data="{StaticResource Image_Shield}" />
                        <Label Margin="20,0,0,0" Padding="0" Content="Restart as admin" />
                    </Grid>
                </Button>
            </WrapPanel>
        </Border>
        <Border Style="{StaticResource ToolBarPanel}">
            <Grid>
                <WrapPanel HorizontalAlignment="Left">
                    <ToggleButton IsChecked="{Binding IsTrackingEnabled}">
                        <Grid>
                            <Path HorizontalAlignment="Left" Width="10" Stretch="Uniform" Fill="Green">
                                <Path.Style>
                                    <Style TargetType="Path">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsTrackingEnabled, FallbackValue=True}" Value="True">
                                                <Setter Property="Data" Value="{StaticResource Image_Stop}" />
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding IsTrackingEnabled}" Value="False">
                                                <Setter Property="Data" Value="{StaticResource Image_Start}" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Path.Style>
                            </Path>
                            <TextBlock Margin="15,0,0,0" Text="Auto-refresh" />
                        </Grid>
                    </ToggleButton>
                    <ToggleButton IsChecked="{Binding IsFullRouteDisplayed}">
                        <Grid>
                            <Path HorizontalAlignment="Left" Height="14" Stretch="Uniform" Fill="#FF6C6CD4" Data="{StaticResource Image_FullRoute}" />
                            <TextBlock Margin="20,0,0,0" Text="Full routes" />
                        </Grid>
                    </ToggleButton>
                    <ToggleButton IsChecked="{Binding IsAerial}">
                        <Grid>
                            <Path HorizontalAlignment="Left" Height="14" Stretch="Uniform" Fill="Coral" Data="{StaticResource Image_MapAerial}" />
                            <TextBlock Margin="20,0,0,0" Text="Aerial mode" />
                        </Grid>
                    </ToggleButton>
                    <TextBlock VerticalAlignment="Center" Text="{Binding Connections.Count, StringFormat='Active connection(s): {0}', FallbackValue='No active connection'}" />
                </WrapPanel>
                <WrapPanel HorizontalAlignment="Right">
                    <Border BorderBrush="White" Background="White" BorderThickness="0" CornerRadius="8" Padding="5,2" Margin="0,0,5,0">
                        <WrapPanel>
                            <Path Fill="#FFA2A2FF" Data="{StaticResource Image_UpdateRate}"/>
                            <Label Margin="4,0" Content="Update rate:" />
                            <ComboBox IsEditable="False" Width="70" ItemsSource="{Binding Intervals, StringFormat='\{0\}s'}" SelectedValue="{Binding Interval}" />
                        </WrapPanel>
                    </Border>
                    <ToggleButton x:Name="btnHelp">
                        <Path Fill="Chocolate" Data="{StaticResource Image_Help}"/>
                    </ToggleButton>
                </WrapPanel>
            </Grid>
        </Border>
        <Grid>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="2" />
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <maps:Map x:Name="CurrentMap" BorderThickness="0" CredentialsProvider="AsuL3JPMhN5U5z09JOCSXp5C_4J_X6YV3FTk7jEMCDLkQdGee7xPspAuIAxbhyyK" Mode="{Binding Mode}">
                    <maps:MapItemsControl x:Name="layerRoutes" ItemsSource="{Binding ConnectionsRoutes}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <maps:MapPolyline Stroke="Blue" StrokeLineJoin="Round" StrokeThickness="1">
                                    <maps:MapPolyline.Style>
                                        <Style TargetType="maps:MapPolyline">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding IsFullRouteDisplayed, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Page}}" Value="True">
                                                    <Setter Property="Locations" Value="{Binding FullRoute}" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding IsFullRouteDisplayed, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Page}}" Value="False">
                                                    <Setter Property="Locations" Value="{Binding RayCoordinates}" />
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </maps:MapPolyline.Style>
                                </maps:MapPolyline>

                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </maps:MapItemsControl>
                    <maps:MapLayer>
                        <Ellipse x:Name="initialPoint" Width="10" Height="10" Fill="Red" maps:MapLayer.PositionOffset="-5,-5" />
                    </maps:MapLayer>
                    <maps:MapItemsControl ItemsSource="{Binding ConnectionsRoutes}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Ellipse Fill="{Binding Brush}" maps:MapLayer.Position="{Binding Coordinates}">
                                    <Ellipse.Style>
                                        <Style TargetType="Ellipse">
                                            <Setter Property="Width" Value="10" />
                                            <Setter Property="Height" Value="10" />
                                            <Setter Property="maps:MapLayer.PositionOffset" Value="-5,-5" />
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding IsSelected}" Value="True">
                                                    <Setter Property="Panel.ZIndex" Value="100" />
                                                    <Setter Property="maps:MapLayer.PositionOffset" Value="-10,-10" />
                                                    <Setter Property="Width" Value="20" />
                                                    <Setter Property="Height" Value="20" />
                                                    <Setter Property="Stroke" Value="{DynamicResource AccentColorBrush}" />
                                                    <Setter Property="StrokeThickness" Value="5" />
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Ellipse.Style>
                                </Ellipse>

                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </maps:MapItemsControl>
                </maps:Map>
                <Border HorizontalAlignment="Center" VerticalAlignment="Center" Background="LightYellow">
                    <StackPanel Margin="30,10" x:Name="ProgressStack">
                        <TextBlock Text="Loading database..." />
                        <ProgressBar x:Name="Progress" Margin="0,10,0,0"  IsIndeterminate="True" Height="5" HorizontalAlignment="Stretch" />
                    </StackPanel>
                </Border>
                <GridSplitter Grid.Column="1" />
                <DockPanel Grid.Column="2">
                    <Border Style="{StaticResource ToolBarPanel}" Background="LightBlue">
                        <UniformGrid Rows="1" Columns="2">
                            <RadioButton GroupName="rdbGroupBy" Content="Group by owner" x:Name="btnGrpOwner" IsChecked="True" Checked="btnGrpOwner_Checked" />
                            <RadioButton GroupName="rdbGroupBy" Content="Group by IP" x:Name="btnGrpIP" Checked="btnGrpIP_Checked"/>
                        </UniformGrid>
                    </Border>
                    <ScrollViewer>
                        <ListView SelectionMode="Single" Width="220" BorderThickness="1,0,0,0" ItemsSource="{Binding ConnectionsView}">
                            <ListView.Resources>
                                <Style TargetType="ListViewItem">
                                    <Setter Property="IsSelected" Value="{Binding IsSelected}" />
                                </Style>
                            </ListView.Resources>
                            <ListView.GroupStyle>
                                <GroupStyle>
                                    <GroupStyle.HeaderTemplate>
                                        <DataTemplate>
                                            <Border Background="LightGray" Padding="5">
                                                <Grid>
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="Auto" />
                                                        <ColumnDefinition  />
                                                    </Grid.ColumnDefinitions>
                                                    <Image Width="16" Stretch="Uniform" Margin="2,0">
                                                        <Image.Style>
                                                            <Style TargetType="Image">
                                                                <Style.Triggers>
                                                                    <DataTrigger Binding="{Binding IsChecked,ElementName=btnGrpOwner}" Value="True">
                                                                        <Setter Property="Source" Value="{Binding Items[0].Icon,Mode=OneTime}" />
                                                                    </DataTrigger>
                                                                </Style.Triggers>
                                                            </Style>
                                                        </Image.Style>
                                                    </Image>
                                                    <TextBlock Grid.Column="1" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis" Text="{Binding Name,Mode=OneTime}">
                                                <Run Text="{Binding Items.Count,Mode=OneWay,StringFormat=' ({0})'}"/>
                                                    </TextBlock>
                                                </Grid>
                                            </Border>
                                        </DataTemplate>
                                    </GroupStyle.HeaderTemplate>
                                </GroupStyle>
                            </ListView.GroupStyle>
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <WrapPanel>
                                        <Path Grid.Column="0" ToolTip="{Binding LastError}" Height="16" Stretch="Uniform" Fill="OrangeRed" Data="F1 M 58.5832,55.4172L 17.4169,55.4171C 15.5619,53.5621 15.5619,50.5546 17.4168,48.6996L 35.201,15.8402C 37.056,13.9852 40.0635,13.9852 41.9185,15.8402L 58.5832,48.6997C 60.4382,50.5546 60.4382,53.5622 58.5832,55.4172 Z M 34.0417,25.7292L 36.0208,41.9584L 39.9791,41.9583L 41.9583,25.7292L 34.0417,25.7292 Z M 38,44.3333C 36.2511,44.3333 34.8333,45.7511 34.8333,47.5C 34.8333,49.2489 36.2511,50.6667 38,50.6667C 39.7489,50.6667 41.1666,49.2489 41.1666,47.5C 41.1666,45.7511 39.7489,44.3333 38,44.3333 Z ">
                                            <Path.Style>
                                                <Style TargetType="Path">
                                                    <Setter Property="Visibility" Value="Collapsed"/>
                                                    <Style.Triggers>
                                                        <DataTrigger Binding="{Binding IsAccessDenied}" Value="True">
                                                            <Setter Property="Visibility" Value="Visible" />
                                                        </DataTrigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </Path.Style>
                                        </Path>
                                        <Image Width="16" Stretch="Uniform" Margin="2,0">
                                            <Image.Style>
                                                <Style TargetType="Image">
                                                    <Setter Property="Visibility" Value="Collapsed" />
                                                    <Style.Triggers>
                                                        <DataTrigger Binding="{Binding IsChecked,ElementName=btnGrpOwner}" Value="False">
                                                            <Setter Property="Source" Value="{Binding Icon,Mode=OneTime}" />
                                                            <Setter Property="Visibility" Value="Visible" />
                                                        </DataTrigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </Image.Style>
                                        </Image>
                                        <TextBlock Foreground="{Binding Brush}">
                                            <TextBlock.Style>
                                                <Style TargetType="TextBlock">
                                                    <Setter Property="Text" Value="{Binding RemoteAddress}" />
                                                    <Style.Triggers>
                                                        <DataTrigger Binding="{Binding IsChecked,ElementName=btnGrpOwner}" Value="False">
                                                            <Setter Property="Text" Value="{Binding Owner}" />
                                                        </DataTrigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </TextBlock.Style>
                                        </TextBlock>
                                    </WrapPanel>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                    </ScrollViewer>
                </DockPanel>
            </Grid>
            <Border Style="{StaticResource InfoBlock}">
                <TextBlock>
            This screen displays all active connections on this computer, updated every second (by default).
Closed connections (in orange) will remain displayed a few seconds before disappearing.
                </TextBlock>
            </Border>
        </Grid>
    </DockPanel>
</Page>
